﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HenIT.CAGCompare
{
    public partial class EditMachineNames : Form
    {
        public EditMachineNames()
        {
            InitializeComponent();
        }

        public List<string> SelectedMachines { get; set; }

        public void AddSelectedMachine(string machineName)
        {
            machineName = machineName.ToUpper();
            if (SelectedMachines == null)
                SelectedMachines = new List<string>();

            if (!SelectedMachines.Contains(machineName))
            {
                if (!Properties.Settings.Default.PreviousMachineList.Contains(machineName))
                {
                    Properties.Settings.Default.PreviousMachineList.Add(machineName);
                    if (NetTools.IsValidMachineName(machineName))
                    {
                        SelectedMachines.Add(machineName);
                    }
                }
                else
                {
                    SelectedMachines.Add(machineName);
                }                
            }
        }

        private void EditMachineNames_Load(object sender, EventArgs e)
        {
            if (SelectedMachines == null)
                SelectedMachines = new List<string>();
            if (Properties.Settings.Default.PreviousMachineList != null)
            {
                List<string> machines = new List<string>();
                foreach (string machineName in Properties.Settings.Default.PreviousMachineList)
                {
                    if (machineName.Trim().Length > 0)
                        if (!machines.Contains(machineName.ToUpper()))
                            machines.Add(machineName.ToUpper());
                }
                machines.Sort();
                foreach (string machineName in machines)
                {
                    ListViewItem lvi = new ListViewItem(machineName);
                    lvi.Checked = SelectedMachines.Contains(machineName);
                    lvwMachines.Items.Add(lvi);
                }
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (lvwMachines.CheckedItems.Count < 2)
                MessageBox.Show("Please select at lease 2 machines to compare!", "Select", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else
            {
                SelectedMachines = new List<string>();
                foreach (ListViewItem machineItem in lvwMachines.CheckedItems)
                {
                    SelectedMachines.Add(machineItem.Text);
                }

                Properties.Settings.Default.PreviousMachineList = new System.Collections.Specialized.StringCollection();
                foreach (ListViewItem machineItem in lvwMachines.Items)
	            {
                    Properties.Settings.Default.PreviousMachineList.Add(machineItem.Text);
                }
                Properties.Settings.Default.Save();

                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            string newMachineName = txtMachineName.Text.Trim().ToUpper();
            if (newMachineName.Length > 0)
            {
                foreach (ListViewItem machineItem in lvwMachines.Items)
                {
                    if (machineItem.Text == newMachineName)
                    {
                        MessageBox.Show("The computer named '" + newMachineName + "' is already in the list!!",
                            "Adding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }

                try
                {
                    if (System.Net.Dns.GetHostAddresses(newMachineName).Length > 0)
                    {
                        ListViewItem lvi = new ListViewItem(newMachineName);
                        lvi.Checked = true;
                        lvwMachines.Items.Add(lvi);
                        txtMachineName.Text = "";
                        txtMachineName.Focus();
                    }
                    else
                    {
                        MessageBox.Show("The computer named '" + newMachineName + "' does not exist or cannot be accessed!",
                            "Adding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (System.Net.Sockets.SocketException)
                {
                    MessageBox.Show("The computer named '" + newMachineName + "' does not exist or cannot be accessed!",
                            "Adding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }                
            }
        }

        private void lvwMachines_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (lvwMachines.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show("Are you sure you want to remove these computer(s)?", "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in lvwMachines.SelectedItems)
                        {
                            lvwMachines.Items.Remove(lvi);
                        }
                    }
                }
            }
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvwMachines.Items)
            {
                lvi.Checked = false;
            }
        }

        private void txtMachineName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                cmdAdd_Click(sender, e);
                e.Handled = true;
            }
        }
    }
}
